package cn.com.cfae.iras.doc.analyze.components.integrity;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import cn.com.cfae.iras.doc.analyze.parse.model.PageModel;
import com.google.gson.JsonElement;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Component("I1024")
@Scope("prototype")
public class I1024 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(I1024.class);
    @Autowired
    private HandingData handingData;

    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取I1024组件M");
        List<ComponentResult> componentResults = checkRulesI1024("M-4",getCatalogId(),"M-4-1",getRuleTitle(),"(M15)");
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取I1024组件BM");
        List<ComponentResult> componentResults = checkRulesI1024("BM-4","BM-4","BM-4-1",getRuleTitle(),"(M15)");
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        log.info("获取I1024组件DM");
        List<ComponentResult> componentResults = checkRulesI1024("DM-4","DM-4","DM-4-1",getRuleTitle(),"(D15)");
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        log.info("获取I1024组件BDM");
        List<ComponentResult> componentResults = checkRulesI1024("BDM-4","BDM-4","BDM-4-1",getRuleTitle(),"(D15)");
        return componentResults;
    }

    private List<ComponentResult> checkRulesI1024(String model,String catalogId,String chapterId,String ruleName,String subTitle){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        ExtractItem extractItem = null;
        int startPage = 0;
        int keywordPageNum = 0;
        List<ComponentResult.TextPosition> contentAndPositionList = null;
        boolean keywordFlag = false;
        String[] keywords = new String[]{"棚户区改造规划","证照","资金监管"};
       /* String phqgzRegex = ".*(棚户区改造|保障房).*(规划|计划).*";
        String szRegex =".*(证照|四证).*";
        String zjjgRegex = ".*(资金|专户).*监管.*";*/
        boolean phqgzFlag = false;
        boolean szFlag = false;
        boolean zjjgFlag = false;
        String phqgzKwd = "棚户区改造规划,棚户区改造计划,保障房规划,保障房计划";
        String szKwd = "证照,四证";
        String zjjgKwd = "资金监管,专户监管";
        try {

            if (extractData!=null){
                ChapterModel chapterModel = extractData.getChapterModel(model);
                startPage = chapterModel!=null?chapterModel.getStartPage():0;
                if (chapterModel!=null){
                    List<PageModel> pageModelList = chapterModel.getPageModelList();
                    if (CollectionUtils.isNotEmpty(pageModelList)){
                        for (PageModel pageModel:pageModelList){
                            Map<String, Object> containsKeywords2LiensOfVL = handingData.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), phqgzKwd);
                            phqgzFlag = (boolean)containsKeywords2LiensOfVL.get("flag");
                            if (phqgzFlag){
                                keywordPageNum = pageModel.getPageNumber();
                                keywordPageNum = keywordPageNum!=0?keywordPageNum:startPage;
                                contentAndPositionList = (List<ComponentResult.TextPosition>)containsKeywords2LiensOfVL.get("position");
                                break;
                            }
                        }
                        if (phqgzFlag){
                            ComponentResult result = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"关键词检查-棚户区改造规划", "正确", false, getRegulationId(), keywordPageNum, contentAndPositionList, "01");
                            componentResults.add(result);
                        }else {
                            ComponentResult result = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"关键词检查-棚户区改造规划", "缺失", true, getRegulationId(), startPage, null, "02");
                            componentResults.add(result);
                        }


                        for (PageModel pageModel:pageModelList){

                            Map<String, Object> containsKeywords2LiensOfVL = handingData.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), szKwd);
                            szFlag = (boolean)containsKeywords2LiensOfVL.get("flag");
                            if (szFlag){
                                keywordPageNum = pageModel.getPageNumber();
                                keywordPageNum = keywordPageNum!=0?keywordPageNum:startPage;
                                contentAndPositionList = (List<ComponentResult.TextPosition>)containsKeywords2LiensOfVL.get("position");
                                break;
                            }
                        }
                        if (szFlag){
                            ComponentResult result = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"关键词检查-证照", "正确", false, getRegulationId(), keywordPageNum, contentAndPositionList, "01");
                            componentResults.add(result);
                        }else {
                            ComponentResult result = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"关键词检查-证照", "缺失", true, getRegulationId(), startPage, null, "02");
                            componentResults.add(result);
                        }


                        for (PageModel pageModel:pageModelList){

                            Map<String, Object> containsKeywords2LiensOfVL = handingData.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), zjjgKwd);
                            zjjgFlag = (boolean)containsKeywords2LiensOfVL.get("flag");
                            if (zjjgFlag){
                                keywordPageNum = pageModel.getPageNumber();
                                keywordPageNum = keywordPageNum!=0?keywordPageNum:startPage;
                                contentAndPositionList = (List<ComponentResult.TextPosition>)containsKeywords2LiensOfVL.get("position");
                                break;
                            }
                        }
                        if (zjjgFlag){
                            ComponentResult result = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"关键词检查-资金监管", "正确", false, getRegulationId(), keywordPageNum, contentAndPositionList, "01");
                            componentResults.add(result);
                        }else {
                            ComponentResult result = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"关键词检查-资金监管", "缺失", true, getRegulationId(), startPage, null, "02");
                            componentResults.add(result);
                        }

                    }
                }
            }
            if (CollectionUtils.isEmpty(componentResults)){
                for (String key :keywords){
                    ComponentResult result = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"关键词检查-"+key, "未识别,建议人工核对", true, getRegulationId(), startPage, null, "03");
                    componentResults.add(result);
                }
            }
        }catch (Exception e){
            componentResults.clear();
            for (String key :keywords){
                ComponentResult result = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"关键词检查-"+key, "未识别,建议人工核对", true, getRegulationId(), startPage, null, "03");
                componentResults.add(result);
            }
              log.error(e.getMessage(),e);
        }


    return componentResults;
    }
}
